//
//  SocketChannelPool.m
//  socket
//
//  Created by  user on 11-10-22.
//  Copyright 2011 GuangZhouXuanWu. All rights reserved.
//

#import "RequestChannelPool.h"

//#import "NSDateExtend.h"

#import "OperationQueueShell.h"

@interface CRunLoopThread : NSThread
{
    NSRunLoop *_runloop;
    
    dispatch_semaphore_t _semaphore;
}

@property(nonatomic, readonly) NSRunLoop *runloop;

@end

@implementation CRunLoopThread

static CRunLoopThread *s_runloopthread;

@synthesize runloop = _runloop;

- (void)main
{
    @autoreleasepool
    {
        _runloop = [NSRunLoop currentRunLoop];
        
        [NSTimer scheduledTimerWithTimeInterval:DBL_MAX target:self selector:@selector(ignore:) userInfo:nil repeats:NO];
        
        if (_semaphore != NULL)
            dispatch_semaphore_signal(_semaphore);
        
        [_runloop run];
    }
}
        
- (void)start
{
    if (_semaphore == NULL)
        _semaphore = dispatch_semaphore_create(0);
    
    [super start];
    
    dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
    
    if (_semaphore != NULL)
        dispatch_release_mm(_semaphore);
    _semaphore = NULL;
}

+ (void)initThreadRunLoop
{
    if (s_runloopthread == nil)
        s_runloopthread = [CRunLoopThread new];
}

+ (void)start
{
    static BOOL b = NO;
    if (b == YES)
        return;
    b = YES;
    [CRunLoopThread initThreadRunLoop];
    [s_runloopthread start];
}

+ (CRunLoopThread *)runLoopThread
{
    return s_runloopthread;
}

- (void)dealloc
{
    superdealloc_mm;
}

@end

@implementation CRequestChannelPool
{
    NSMutableArray *_arQueue;
    
    XMSDispatchQueueWrapper *_lock;
    
    BOOL _bRunning;
}

//@synthesize m_curretnrunloop;
//@synthesize m_runloopthread;

- (id)init
{
    self=[super init];

    _arQueue = [NSMutableArray new];
    
    _lock = [[XMSDispatchQueueWrapper alloc]initWithName:""];

    return self;
}

//- (BOOL)AddSocketChannelConnect:(CSocketChannel2 *)socket
//{
//    __block BOOL bResult = FALSE;
//
//    DispatchThreadSync(m_lock,m_lockqueuekey, ^
//    {
//        if (m_bRunning == YES)
//        {
//            if (nil != m_arQueue)
//            {
//                socket.channelPool = self;
//                int nResult = [socket initSocketChannel];
//                if (YES != nResult)
//                {
//                    [socket cancelRequest];
//                    bResult = FALSE;
//                }
//                else
//                {
//                    [m_arQueue addObject:socket];
//                    bResult = YES;
//                }
//            }
//        }
//    });
//
//    return bResult;
//}

//- (CSocketChannel2 *)AddSocketChannelConnect:(NSString *)szIP port:(int)nPort delegate:(id)delegate
//{
//    return nil;
//}

- (BOOL)addRequestChannel:(CRequestChannel *(^)())channel
{
    __block BOOL bResult = FALSE;

    [_lock dispatchThreadSync:^
    {
        if (_bRunning == YES)
        {
            if (nil != _arQueue)
            {
                CRequestChannel *rc = channel();
                if(rc != nil)
                {
                    rc.channelPool = self;
                    [_arQueue addObject:rc];
                    bResult = YES;
                }

            }
        }
    }];
    return bResult;
}

- (void)removeRequestChannel:(CRequestChannel*(^)())request
{
    if (request != NULL)
    {
        [_lock dispatchThreadSync:^
        {
            CRequestChannel *r = request();
            if(r != nil)
            {
                [_arQueue removeObject:r];
            }
        }];
    }
    
}

- (void)cancelRequestChannel:(CRequestChannel *)request
{
    if (request != nil)
    {
        [_lock dispatchThreadSync:^
        {
            [request cancelRequest];
        }];
    }
}

- (void)Suspend
{
//    CFRunLoopStop([m_curretnrunloop getCFRunLoop]);
}

- (void)Resume
{
//    CFRunLoopWakeUp([m_curretnrunloop getCFRunLoop]);
}

- (void)startRequestRunLoop
{
    if (_bRunning == YES)
        return;

    _bRunning = YES;

    [CRunLoopThread start];

    if (nil == _arQueue)
        _arQueue = [NSMutableArray new];
}

- (void)stopRequestRunLoop
{
    [_lock dispatchThreadSync:^
    {
        _bRunning = NO;

        for (CRequestChannel *sc in _arQueue)
        {
            [sc cancelRequest];
        }

        [_arQueue removeAllObjects];
    }];
}

- (void)cancelRequest
{
    [_lock dispatchThreadSync:^
    {
        for (CRequestChannel *sc in _arQueue)
        {
            [sc cancelRequest];
        }

        [_arQueue removeAllObjects];
    }];
}

//-(BOOL)GetRequestChannelPoolRunLoop:(void(^)(NSRunLoop* runloop))complete
//{
//    BOOL bResult=NO;
//    if(m_bRunning==YES/*&&m_runloopthread.m_bRunning==YES&&m_runloopthread.m_bInprogress==YES*/)
//    {
//        complete([CRunLoopThread runLoopThread].m_runloop);
//        bResult=YES;
//    }
//    return bResult;
//}
//
//-(BOOL)GetRequestChannelPoolRunLoopTread:(void(^)(NSThread* thread))complete
//{
//    BOOL bResult=NO;
//    if(m_bRunning==YES/*&&m_runloopthread.m_bRunning==YES&&m_runloopthread.m_bInprogress==YES*/)
//    {
//        complete([CRunLoopThread runLoopThread]);
//        bResult=YES;
//    }
//    return bResult;
//}

- (BOOL)addRequestChannelPoolRunLoop:(void (^)(NSRunLoop *runloop))complete
{
    BOOL bResult = NO;
    if (_bRunning == YES)
    {
        complete([CRunLoopThread runLoopThread].runloop);
        bResult = YES;
    }
    return bResult;
}

- (BOOL)addRequestChannelPoolRunLoopTread:(void (^)(NSThread *thread))complete
{
    BOOL bResult = NO;
    if (_bRunning == YES)
    {
        complete([CRunLoopThread runLoopThread]);
        bResult = YES;
    }
    return bResult;
}

- (void)removeRequestChannelPoolRunLoop:(void (^)(NSRunLoop *runloop))complete
{
    complete([CRunLoopThread runLoopThread].runloop);
}

- (void)removeRequestChannelPoolRunLoopTread:(void (^)(NSThread *thread))complete
{
    complete([CRunLoopThread runLoopThread]);
}

- (void)dealloc
{
    [_lock release_mm];
    [_arQueue release_mm];
    superdealloc_mm;
}

@end
